const connection = require("../app/database");

class UserService {
  //创建
  async create(user) {
    //写入数据库

    //解构
    const { name, password } = user;

    //预编译语句
    const statement = "INSERT INTO `user` (name, password) VALUES (?, ?);";

    //插入数据库
    const [res] = await connection.execute(statement, [name, password]);
    return res;
  }

  //查询
  async findUserByName(name) {
    //预编译语句
    const statement = "SELECT * FROM `user` WHERE name = ?;";

    //数据库查询
    const [res] = await connection.execute(statement, [name]);
    return res;
  }

  //更新用户头像
  async updateAvatarUrlById(avatarUrl, userId) {
    const statement = `UPDATE user SET avatar_url = ? WHERE id = ?;`;
    const [result] = await connection.execute(statement, [avatarUrl, userId]);
    return result;
  }
}

module.exports = new UserService();
